package com.fz.song.controller;


import com.fz.song.common.Constants;
import com.fz.song.enums.ExceptionEnum;
import com.fz.song.exception.CustomException;
import com.fz.song.pojo.SysRole;
import com.fz.song.pojo.SysRoleUser;
import com.fz.song.pojo.SysUser;
import com.fz.song.service.RoleService;
import com.fz.song.service.UserService;
import com.fz.song.util.DateUtils;
import com.fz.song.util.MD5;
import com.fz.song.util.MD5Util;
import com.fz.song.util.NumberUtil;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Controller
@RequestMapping("usercontroller")
public class UserController extends BaseController {

    @Autowired
    public UserService userService;
    @Autowired
    public RoleService roleService;


    @RequestMapping("users")
    public String users(HttpServletRequest request, HttpServletResponse response) {
        //页面参数
        return "user/users";
    }

    @RequestMapping("users_list")
    public String users_list(@RequestParam(value = "page", defaultValue = "1") int page, HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> params = new HashMap();
        String condition = request.getParameter("condition");
        String status = request.getParameter("status");
        params.put("condition", condition);
        params.put("status", status);
        PageInfo<SysUser> datalist = userService.queryUserByPage(params, page, Constants.LIST_SIZE);
        request.setAttribute("datalist", datalist);
        return "user/users_list";
    }

    @RequestMapping("users_getUser")
    @ResponseBody
    public Object users_getUser(HttpServletRequest request, HttpServletResponse response) throws CustomException {
        int sysid = NumberUtil.Str2Int(request.getParameter("sysid"), -1);
        if (sysid == -1) {
            ExceptionEnum paramException = ExceptionEnum.PARAM_EXCEPTION;
            throw new CustomException(paramException.getCode(), paramException.getMessage());
        }
        SysUser sysUser = userService.getUserById(sysid);
        if (sysUser == null) {
            return result(0, "获取用户失败");
        } else {
            return result(1, "获取用户成功", sysUser);
        }


    }

    @RequestMapping("users_saveUser")
    @ResponseBody
    public Object users_saveUser(HttpServletRequest request, HttpServletResponse response) throws CustomException {

        String loginPassword = Constants.LOGIN_PASSWORD;
        loginPassword = MD5Util.MD5(loginPassword);
        int sysid = NumberUtil.Str2Int(request.getParameter("sysid"), -1);
        String loginname = request.getParameter("loginname");
        String nickname = request.getParameter("nickname");
        String state = request.getParameter("state");
        String mobile = request.getParameter("mobile");
        String email = request.getParameter("email");
        String description = request.getParameter("description");
        if (sysid == -1) {
            ExceptionEnum paramException = ExceptionEnum.PARAM_EXCEPTION;
            throw new CustomException(paramException.getCode(), paramException.getMessage());
        }
        SysUser sysUser = new SysUser();
        if (sysid == 0) {
            sysUser.setSysid(0);
            sysUser.setCreatetime(DateUtils.getTimeNow());
            sysUser.setPassword(loginPassword);
        } else {
            sysUser = userService.getUserById(sysid);
        }
        sysUser.setUpdatetime(DateUtils.getTimeNow());
        sysUser.setLoginname(loginname);
        sysUser.setNickname(nickname);
        sysUser.setState(NumberUtil.Str2Int(state, 1000));
        sysUser.setMobile(mobile);
        sysUser.setEmail(email);
        sysUser.setDescription(description);

        int num = userService.saveUser(sysUser);
        if (num == 1)
            return result(1, "操作成功");
        return result(0, "操作失败");


    }

    @RequestMapping("users_assignRole")
    public String users_assignRole(HttpServletRequest request, HttpServletResponse response) {
        int sysid = NumberUtil.Str2Int(request.getParameter("sysid"), 0);
        List<SysRole> roleList = roleService.getRoleListAll();
        List<SysRoleUser> roleUserlist = roleService.getUserRole(sysid);

        for (SysRole role : roleList) {
            Integer roleid = role.getSysid();
            for (SysRoleUser roleUser : roleUserlist) {
                Integer userroleid = roleUser.getRoleid();
                if (userroleid == roleid) {
                    role.setIshave(true);
                    break;
                }
            }

        }
        request.setAttribute("roles", roleList);
        request.setAttribute("sysid", sysid);
        return "user/users_assignrole";
    }

    @RequestMapping("users_saveRole")
    @ResponseBody
    public Object users_saveRole(HttpServletRequest request, HttpServletResponse response) {
        int sysid = NumberUtil.Str2Int(request.getParameter("sysid"), 0);
        String roids = request.getParameter("roids");

        Boolean flag = roleService.saveUserRole(sysid,roids);
        if (flag)
            return  result(1, "分配成功");
        return result(0, "分配失败");
    }

    @RequestMapping("users_changeStatus")
    @ResponseBody
    public Object users_changeStatus(HttpServletRequest request, HttpServletResponse response) {
        int sysid = NumberUtil.Str2Int(request.getParameter("sysid"), 0);
        int status = NumberUtil.Str2Int(request.getParameter("status"), 1000);
        int  num=userService.changeStatus(sysid,status);
        if (num==1)
            return  result(1, "操作成功");
        return result(0, "操作成功");
    }


}
